﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit Role
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Edit Role</h2>
<script type="text/javascript">
    function changeInputValue(e, inputid) {
        var value = e.checked;
        $("#"+inputid).val(value);
    }

    function SelectAllcontrollers(colomnName) {
        // viewselectall viewcb viewin
        if ($('#' + colomnName + 'selectall').is(':checked')) {
            $("." + colomnName + "cb").attr("checked", "true");
            $("." + colomnName + "in").val(true);
        }
        else {
            $("." + colomnName + "cb").removeAttr("checked");
            $("." + colomnName + "in").val(false);
        }
    }
 </script>

<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>System Role</legend>
        <br />
        
        <table>
        <tr><td style="width:80px">
        <div class="editor-label">
            Role Name
        </div></td><td>
        <div class="editor-field">
            <%: ViewBag.Role%>
        </div></td></tr>
        
        <tr><td colspan="2">
        <table><tr><%if (ViewBag.Role != "Administrator")
                     { %><td style="vertical-align:top;">
        <fieldset>
        <legend>Role Privileges</legend>
        <br />
         <table>
         <tr><td><b>Screen Name</b></td><td><b>View</b></td><td><b>Add</b></td><td><b>Edit</b></td><td><b>Delete</b></td></tr>
         <tr><td></td><td><input id="viewselectall" type='checkbox' onclick='SelectAllcontrollers("view");'/> All</td><td><input id="addselectall" type='checkbox' onclick='SelectAllcontrollers("add");'/> All</td><td><input id="editselectall" type='checkbox' onclick='SelectAllcontrollers("edit");'/> All</td><td><input id="deleteselectall" type='checkbox' onclick='SelectAllcontrollers("delete");'/> All</td></tr>
        <%
                         IEnumerable<Smartec.Accounts.Camel.AMS.Model.Entities.RoleController> roleControllers = Smartec.Accounts.Camel.AMS.Model.Facades.RoleControllerFacade.GetRoleControllers(ViewBag.Role);

                         var amsControllers = Smartec.Accounts.Camel.AMS.Model.Facades.RoleControllerFacade.FindAllControllers().Select(o => new
                          {
                              ID = o.ID,
                              Name = o.DisplayName
                          ,
                              View = roleControllers.Any(rc => rc.Controller_ID == o.ID) ? roleControllers.Where(rc => rc.Controller_ID == o.ID).First().View : false
                          ,
                              Add = roleControllers.Any(rc => rc.Controller_ID == o.ID) ? roleControllers.Where(rc => rc.Controller_ID == o.ID).First().Add : false
                          ,
                              Edit = roleControllers.Any(rc => rc.Controller_ID == o.ID) ? roleControllers.Where(rc => rc.Controller_ID == o.ID).First().Edit : false
                          ,
                              Delete = roleControllers.Any(rc => rc.Controller_ID == o.ID) ? roleControllers.Where(rc => rc.Controller_ID == o.ID).First().Delete : false
                          });
                         foreach (var controller in amsControllers)
                         {%>
        <tr><td><%:controller.Name%><input type="hidden" name="Controller_ID" value="<%:controller.ID%>"/></td>
        <td><input type="checkbox" class="viewcb" <%:controller.View?"checked":""%> onchange='<%: string.Format("changeInputValue(this,'vin_{0}')", controller.ID)%>'/>
        <input type="hidden" class="viewin" id='<%: "vin_"+controller.ID%>' value="<%:controller.View%>" name="RCView"/></td>
        <td><input type="checkbox" class="addcb" <%:controller.Add?"checked":""%> onchange='<%: string.Format("changeInputValue(this,'ain_{0}')", controller.ID)%>'/>
        <input type="hidden" class="addin" id='<%: "ain_"+controller.ID%>' value="<%:controller.Add%>" name="RCAdd"/></td>
        <td><input type="checkbox" class="editcb" <%:controller.Edit?"checked":""%> onchange='<%: string.Format("changeInputValue(this,'ein_{0}')", controller.ID)%>'/>
        <input type="hidden" class="editin" id='<%: "ein_"+controller.ID%>' value="<%:controller.Edit%>" name="RCEdit"/></td>
        <td><input type="checkbox" class="deletecb" <%:controller.Delete?"checked":""%> onchange='<%: string.Format("changeInputValue(this,'din_{0}')", controller.ID)%>'/>
        <input type="hidden" class="deletein" id='<%: "din_"+controller.ID%>' value="<%:controller.Delete%>" name="RCDelete"/></td>
       </tr>
        <%} %></table>

        </fieldset></td><td></td><%} %><td style="vertical-align:top;"><fieldset>
        <legend>Role Users</legend>
        <br />
        <table>
        <%foreach (KeyValuePair<string, KeyValuePair<string, bool>> user in ViewBag.AllUsers)
          {%>
        <tr><td><input type="checkbox" name="roleuser" value="<%:user.Key%>" <%:user.Value.Value?"checked":""%>/></td><td><%:user.Value.Key%></td></tr>
        <%} %></table>
        </fieldset></td></tr></table>
        </td></tr>
        <tr><td colspan="2">
        <div class="editor-label">
                        <input type="submit" value="Save"/>
                    </div></td></tr>
        </table>
    </fieldset>
<% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="HeaderContent" runat="server">
</asp:Content>
